package com.simon.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.*;
import java.util.Iterator;
import java.util.concurrent.ExecutionException;

/**
 * Created by changf on 2017/7/10.
 */
public class NioServer {
    private ByteBuffer readBuffer;

    private Selector selector;
    public static void main(String[] args) {
        NioServer server = new NioServer();
        server.init();
        System.out.println("server started --> 8383");
        server.listen();
    }

    private void init(){
        readBuffer=ByteBuffer.allocate(1024);
        ServerSocketChannel servSocketChannel;
        try{
            servSocketChannel=ServerSocketChannel.open();

            servSocketChannel.configureBlocking(false);

            servSocketChannel.socket().bind(new InetSocketAddress(8383));

            selector= Selector.open();

            servSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

        }catch (Exception e){
            e.printStackTrace();

        }
    }


    private void listen(){
        while(true){
            try{
                //询问选择器  这边可以设置时间
                selector.select(1000);
                Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
                while(iterator.hasNext()){
                    SelectionKey next = iterator.next();
                    //确保不重复处理
                    iterator.remove();

                    handleKey(next);
                }

            }catch (Exception e){

            }

        }

    }


    public void handleKey(SelectionKey key) throws IOException {

        SocketChannel channel=null;
        if(key.isAcceptable()){
            System.out.println("get connect");
            ServerSocketChannel  serverChannel =(ServerSocketChannel) key.channel();
            //接受连接请求
            channel = serverChannel.accept();
            channel.configureBlocking(false);
            channel.register(selector,SelectionKey.OP_READ);
        }else if(key.isReadable()){
            System.out.println("get read=====");
           channel = (SocketChannel) key.channel();
            readBuffer.clear();
            int read = channel.read(readBuffer);
            if(read>0){
                readBuffer.flip();
                CharBuffer charBuffer = CharsetHelper.decode(readBuffer);
                String question=charBuffer.toString();
                String answer = getAnswer(Integer.valueOf(question));
                channel.write(CharsetHelper.encode(CharBuffer.wrap(answer)));
            }else{
                channel.close();
            }
        }
    }
    private String getAnswer(int question) {
        String answer = null;

        switch (question) {
            case 1:
                answer = "sssssssssss\n";
                break;
            case 2:
                answer = "bbbbbbbbbb\n";
                break;
            case 3:
                answer = "ccccccccccc\n";
                break;
            case 4:
                answer = "hello\n";
                break;
            case 5:
                answer = "88\n";
                break;
            default:
                answer = "dddddddddddddddddddddd";
        }

        return answer;
    }


}
